from pms.common.views import PostFormula
from department.utils import assert_department_permissions
from department.utils.member import add_department_member, department_member_view
from department.models import Department, DepartmentRole
from user.models import User


class API(PostFormula):

    def handle(self, req):
        did = req['did']
        assert_department_permissions(self.user.id, did, [3])
        member = req['member']
        return self.success_respond(
            department_member_view(
                add_department_member(
                    department=Department.objects.get(id=did),
                    department_role=DepartmentRole.objects.get(id=member['role']['id']),
                    user=User.objects.get(id=member['id']),
                    annotation=member['annotation']
                )
            )
        )
